package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "分类管理相关接口")
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /*
    * 分类分页查询
    *
    * */
    @ApiOperation("分类分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分类分页查询{}",categoryPageQueryDTO);
        PageResult pageResult = categoryService.page(categoryPageQueryDTO);
        return Result.success(pageResult);
    }
    /*
    * 新增分类
    * */
    @ApiOperation("新增分类")
    @PostMapping
    public Result save(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类{}",categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();
    }
    /*
    * 开启/关闭某分类
    * */
    @ApiOperation("开启/关闭某分类")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("开启/关闭某分类{},{}",status,id);
        categoryService.stratOrStop(status,id);
        return Result.success();
    }
    /*
    * 根据id删除分类
    * */
    @ApiOperation("根据id删除分类")
    @DeleteMapping
    private Result delete(Long id){
        log.info("删除id:{}的分类",id);
        categoryService.deleteById(id);
        return Result.success();
    }
    /*
    * 根据分类类型查询数据
    * */
    @ApiOperation("根据分类类型查询数据")
    @GetMapping("/list")
    public Result<List<Category>> findByType(Integer type){
        log.info("查找分类为{}的数据",type);
        Category category = new Category();
        category.setType(type);
        List<Category> categoryList = categoryService.find(category);
        return Result.success(categoryList);
    }
    /*
    * 修改分类
    * */
    @ApiOperation("修改分类")
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO){
        log.info("修改分类{}",categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }

}
